package jehc.djshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BasePage;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseSearch;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.sys.model.XtMessage;
import jehc.djshi.sys.service.XtMessageService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
/**
 * @Desc 短消息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtMessage")
@Api(value = "短消息API",tags = "短消息API",description = "短消息API")
public class XtMessageController extends BaseAction {

	@Resource
	private XtMessageService xtMessageService;

	/**
	 * 查询短消息列表并分页
	 * @param baseSearch
	 */
	@PostMapping(value="/list")
	@Auth(value = "/xtMessage/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询短消息列表并分页", notes="查询短消息列表并分页")
	public BasePage<List<XtMessage>> getXtMessageListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		if("0".equals(condition.get("type"))){//我发送的消息
			condition.put("fromId", getXtUid());
		}else if("1".equals(condition.get("type"))){
			condition.put("toTd", getXtUid());
		}else{
			condition.put("toId", "-1");
		}
		commonHPager(baseSearch);
		List<XtMessage> xtMessageList = xtMessageService.getXtMessageListByCondition(condition);
		PageInfo<XtMessage> page = new PageInfo<XtMessage>(xtMessageList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	 * 查询单个短消息
	 * @param id
	 */
	@GetMapping(value="/get/{id}")
	@Auth(value = "/xtMessage/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个短消息", notes="查询单个短消息")
	public BaseResult<XtMessage> getXtMessageById(@PathVariable("id")String id, String type){
		if("1".equals(type)){
			Map<String, Object> condition = new HashMap<String,Object>();
			condition.put("isRead", 1);
			condition.put("readTime", getDate());
			condition.put("id", id);
			xtMessageService.updateRead(condition);
		}
		XtMessage xtMessage = xtMessageService.getXtMessageById(id);
		return outDataStr(xtMessage);
	}

	/**
	 * 添加
	 * @param xtMessage
	 * @param bindingResult
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个短消息", notes="创建单个短消息")
	public BaseResult addXtMessage(@Valid @RequestBody XtMessage xtMessage, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return outAudStr(false,backFem(bindingResult));
		}
		int i = 0;
		if(null != xtMessage){
			xtMessage.setId(toUUID());
			i=xtMessageService.addXtMessage(xtMessage);
		}
		if(i>0){
			return outAudStr(true,"发送成功");
		}else{
			return outAudStr(false,"发送失败");
		}
	}

	/**
	 * 修改
	 * @param xtMessage
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个短消息", notes="编辑单个短消息")
	public BaseResult updateXtMessage(@RequestBody XtMessage xtMessage){
		int i = 0;
		if(null != xtMessage){
			i=xtMessageService.updateXtMessage(xtMessage);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除短消息", notes="删除短消息")
	public BaseResult delXtMessage(String id,String type ){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			condition.put("type",type);
			i=xtMessageService.delXtMessage(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 更新已读状态
	 * @param xtMessage
	 * @return
	 */
	@PutMapping(value="/updateRead")
	@ApiOperation(value="更新已读状态", notes="更新已读状态")
	@Auth(value = "/xtMessage/updateRead",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult updateRead(@RequestBody XtMessage xtMessage){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("isRead", 1);
		condition.put("fromId", getXtUid());
		condition.put("toId", xtMessage.getToId());
		int i = xtMessageService.updateRead(condition);
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 加载历史列表数据并分页
	 * @param baseSearch
	 */
	@PostMapping(value="/his/list")
	@Auth(value = "/xtMessage/his/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="加载历史列表数据并分页", notes="加载历史列表数据并分页")
	public BasePage<List<XtMessage>> getXtMessageHisListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = new HashMap<String, Object>();
		commonHPager(baseSearch);
		condition.put("fromId", getXtUid());
		List<XtMessage> xtMessageList = xtMessageService.getXtMessageListByCondition(condition);
		PageInfo<XtMessage> page = new PageInfo<XtMessage>(xtMessageList);
		condition = new HashMap<String, Object>();
		condition.put("toTd", baseSearch.ConvertToMap().get("toId"));
		condition.put("fromId", getXtUid());
		condition.put("isRead", 1);
		xtMessageService.updateRead(condition);
		return outPageStr(page,baseSearch);
	}

	/**
	 * 获取未读消息个数
	 * @param xtMessage
	 * @return
	 */
	@PostMapping(value="/unRead/count")
	@Auth(value = "/xtMessage/unRead/count",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询未读消息个数", notes="查询未读消息个数")
	public BaseResult<List<XtMessage>> getXtMessageUnReadCount(@RequestBody XtMessage xtMessage){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("toTd", getXtUid());
		condition.put("isRead", 0);
		List<XtMessage> xtMessageList = xtMessageService.getXtMessageCountByCondition(condition);
		return outItemsStr(xtMessageList);
	}
}
